$(function () {
    /**
     * 新增功能
     */
    $('.majorAdd').on('click', function () {
        var flag= true;
        var addtr = '<tr>'
            + '<td></td>'
            + '<td class="hidden"></td>'
            + '<td class="text-center">'
            + '	   <input type="text" value="" class="input-sm form-control majorName">'
            + '</td>'
            + '<td class="text-center">'
            + '  <a href="#" class="btn btn-xs btn-use save"><i class="fa fa-copy"></i> 保存</a>'
            + '  <a href="#" class="btn btn-warning btn-xs cancel"><i class="fa fa-times"></i> 取消</a>'
            + '</td>'
            + '</tr>';
        if ($('#major tbody tr .form-control').length == 0) {
            $('#major').find('tbody').prepend(addtr);
        }

        //保存
        $('#major tbody').on('click', '.save', function () {
        	var majorName = $('#major').find('.majorName').val();
        	if(majorName==""||majorName==null){
        		 toastr.error("请填写专业名称！");
        		 return;
        	}
        	if(majorName.length>=50){
        		toastr.error("专业名称超出长度，请确认！");
        		return;
        	}
            $.ajax({
                url: ctx + '/bizMajor/addNewRecord',
                type: 'POST',
                data: {
                	majorName:majorName
	             },
                dataType: 'json',
                success: function (data) {
                    if (data.status == "success"){
//                    	DataTable.init();
                    	loadHtml('/bizMajor/index');
                    }
                    else
                   	 toastr.error(data.msg);
                },
                error: function (response) {
               	 toastr.error("系统错误");
                }
            });
            $(this).removeClass('save btn-use').addClass('edit btn-info');
            $(this).html('<i class="fa fa-edit"></i> 编辑');
            $(this).next().removeClass('cancel btn-warning').addClass('delete btn-danger');
            $(this).next().html('<i class="fa fa-trash-o"></i> 删除');
        });

        //取消
        $('#major tbody').on('click', '.cancel', function () {
            if (flag) {
                $(this).parent().parent().remove();
                flag = false;
            }
        });
    });

    /**
     * 编辑功能
     */
    $('#major tbody').on('click', '.edit', function () {
        var tds = $(this).parent().siblings();

        //专业名称
        var majorName = tds.eq(2).text();

        tds.eq(0).html('');
        tds.eq(1).html(tds.eq(1).text().trim());
        tds.eq(2).html('<input type="text" value="' + tds.eq(2).text().trim() + '" class="input-sm form-control majorName">');
        

        $(this).removeClass('edit btn-info').addClass('save-bj btn-use');
        $(this).html('<i class="fa fa-copy"></i> 保存');
        $(this).next().removeClass('delete btn-danger').addClass('cancel btn-warning');
        $(this).next().html('<i class="fa fa-times"></i> 取消');

        //取消
        $('#major tbody').on('click', '.cancel', function () {
            tds.eq(0).html('<label><input type="checkbox"><span class="text"></span></label>');
            tds.eq(2).html(majorName);
            $(this).removeClass('cancel btn-warning').addClass('delete btn-danger');
            $(this).html('<i class="fa fa-trash-o"></i> 删除');

            $(this).prev().removeClass('save btn-use').addClass('edit btn-info');
            $(this).prev().html('<i class="fa fa-edit"></i> 编辑');
        });


        //保存
        $('#major tbody').on('click', '.save-bj', function () {
        	var majorId = $(this).parent().siblings().eq(1).text();
        	var majorName = $(this).parent().siblings().eq(2).find(".majorName").val();
        	if(majorName.length>=50){
        		toastr.error("专业名称超出长度，请确认！");
        		return;
        	}
        	$.ajax({
        		url : ctx + '/bizMajor/updateRecord',
        		type : 'POST',
        		dataType: 'json',
        		data : {
        			"majorId":majorId,
        			"majorName":majorName
        		},
        		success : function(data){
        			if(data.status != "success"){
        				toastr.error(data.msg);
        			}else{
        				DataTable.init();
        			}
        		},
        		error : function(){
        			toastr.error("系统错误");
        		}
        	});
        	return false;
        });

    });

    /**
     * 删除功能
     */
    $('#major tbody').on('click', '.delete', function () {
    	var majorId = $(this).parent().siblings().eq(1).text();
    	$.ajax({
    		url : ctx + '/bizMajor/updateRecord',
    		type : 'POST',
    		dataType: 'json',
    		data : {
    			"majorId":majorId,
    			"deleteMark":0
    		},
    		success : function(data){
    			if(data.status != "success"){
    				toastr.error("删除不成功，请重试！");
    			}else{
    				DataTable.init();
    			}
    		},
    		error : function(){
    			toastr.error("系统错误");
    		}
    	});
    	return false;
    });
});

DataTable.init();

function retrieveData( sSource, aoData, fnCallback, oSettings ) {
	aoData.push( { "name": "pageNum", "value": (Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength )+1) });
    aoData.push( { "name": "pageSize", "value": oSettings._iDisplayLength });  
    var searchVal = $('#searchVal').val();
    if(searchVal && searchVal.length != 0){
    	aoData.push({ "name": "searchVal", "value": searchVal});
    }
    $.ajax( {  
        "url": sSource,  
        "data": aoData,  
        "cache": false,  
        "dataType": 'json', 
        "type": "POST", 
        "success" :function(response) {
        	fnCallback(response.returnObject);
        }  
    } );  
}
//回车搜索
function search(){
	if(event.keyCode==13){
		DataTable.init();
	}
}